package teach.shuji.java12.config;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import teach.shuji.java12.system.entity.User;
import teach.shuji.java12.system.mapper.UserMapper;

import java.util.Arrays;
import java.util.List;

// 权限控制-自己实现
@Component
public class SaTokenPermission  implements StpInterface {

    //1.从写好的service取
    //2.或从mapper直接读数据取

    @Autowired
    UserMapper userMapper;

    // 通过用户ID取得权限列表，详细的自己写，此处采用一个字段偷懒
    @Override
    public List<String> getPermissionList(Object userId, String loginType) {
        // 如果自己实现，这儿需要通过取得用户角色，再从角色列表取到角色些拥有的所有权限放入权限列表。
        var query = new LambdaQueryWrapper<User>().eq(User::getId, userId);
        User user = userMapper.selectOne(query);
        if(user == null) return null;
        String[] permissions =  user.getPermissions().split(",");
        return Arrays.stream(permissions).toList();
    }

    @Override
    public List<String> getRoleList(Object userId, String loginType) {
        return null;
    }
}
